package com.idlecode.panel;

import java.util.List;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ControlAdapter;
import org.eclipse.swt.events.ControlEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.TableItem;
import com.idlecode.bean.FieldTypeEntity;
import com.idlecode.service.FieldTypeService;
import cn.hutool.core.collection.CollUtil;

/**
 * @program: IdleCode
 * @description
 * @author: rongjuwei
 * @create: 2024-02-05 17:06
 **/
public class FieldTypeMappingPanel extends Composite {

	private Table table;

	public FieldTypeMappingPanel(Composite parent, int style) {
		super(parent, style);

		setLayout(new GridLayout(1, false));

		table = new Table(this, SWT.BORDER | SWT.FULL_SELECTION);
		table.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
		table.setHeaderVisible(true);
		table.setLinesVisible(true);

		TableColumn fieldTypeColumn = new TableColumn(table, SWT.NONE);
		fieldTypeColumn.setText("字段类型");
		/*
		 * 固定宽度：使用 setWidth(int width) 方法，列的宽度将保持固定。 自动调整宽度：使用 pack() 方法，列的宽度将根据其内容自动调整。
		 */

		TableColumn propertyTypeColumn = new TableColumn(table, SWT.NONE);
		propertyTypeColumn.setText("属性类型");

		TableColumn propertyPackageNameColumn = new TableColumn(table, SWT.NONE);
		propertyPackageNameColumn.setText("属性包名");
		// propertyPackageNameColumn.setAlignment(SWT.CENTER);

		// 添加一个监听器，在表格调整大小时动态调整列宽
		table.addControlListener(new ControlAdapter() {
			@Override
			public void controlResized(ControlEvent e) {
				int tableWidth = table.getClientArea().width;
				fieldTypeColumn.setWidth((int) (tableWidth * 0.3)); // 30%
				propertyTypeColumn.setWidth((int) (tableWidth * 0.25)); // 25%
				propertyPackageNameColumn.setWidth((int) (tableWidth * 0.45)); // 45%
			}
		});

		List<FieldTypeEntity> fieldTypes = FieldTypeService.getFieldTypes();
		if (CollUtil.isNotEmpty(fieldTypes)) {
			for (FieldTypeEntity fieldType : fieldTypes) {
				new TableItem(table, SWT.NONE).setText(new String[] { fieldType.getColumnType(),
						fieldType.getAttrType(), fieldType.getPackageName() });
			}
		}
	}

}
